package com.nuoniu.sibanyun.service.erp.barcode.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.dto.ErpBarcodeRuleSearchDto;
import com.nuoniu.sibanyun.entity.dto.ErpBarcodeRuleSearchMoreDto;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodeRule;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodeRuleBo;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodeRuleContent;
import com.nuoniu.sibanyun.mapper.erp.barcode.ErpBarcodeRuleMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodeRuleBoService;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodeRuleContentService;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodeRuleService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 条码管理-条码规则 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2021-11-08
 */
@Service
public class ErpBarcodeRuleServiceImpl extends ServiceImpl<ErpBarcodeRuleMapper, ErpBarcodeRule> implements IErpBarcodeRuleService {

    @Autowired
    private IErpAccountService erpAccountService;
    @Autowired
    private IErpBarcodeRuleBoService barcodeRuleBoService;
    @Autowired
    private IErpBarcodeRuleContentService barcodeRuleContentService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Long saveRule(ErpBarcodeRule erpBarcodeRule) {
        Integer currentAccountId = erpAccountService.getCurrentAccountId();
        Integer barcodeType = erpBarcodeRule.getBarcodeType();
        if(null == barcodeType)throw new NuoNiuException("请选择 条码类型");
        Integer ruleType = erpBarcodeRule.getRuleType();
        if(null == ruleType)throw new NuoNiuException("请选择 规则类型");
        UserInfo userInfo = UserThreadLocal.get();
        LocalDateTime now = LocalDateTime.now();
        Long mainId = erpBarcodeRule.getId();
        if(null == mainId){
            erpBarcodeRule.setCreateDate(now);
            erpBarcodeRule.setCreateUserId(userInfo.getUserId());
            erpBarcodeRule.setCreateUserName(userInfo.getRealName());
            erpBarcodeRule.setCompanyId(userInfo.getCompanyId());
            erpBarcodeRule.setAccountId(currentAccountId);

        }else {
            erpBarcodeRule.setUpdateDate(now);
            erpBarcodeRule.setUpdateUserId(userInfo.getUserId());
            erpBarcodeRule.setUpdateUserName(userInfo.getRealName());
        }
        boolean b = saveOrUpdate(erpBarcodeRule);
        if(b){
            mainId = erpBarcodeRule.getId();
            int intValue = mainId.intValue();
            erpBarcodeRule.getRuleBos().forEach(f->{
                f.setAccountId(currentAccountId);
                f.setCompanyId(userInfo.getCompanyId());
                f.setBarcodeRuleId(intValue);
            });
            erpBarcodeRule.getRuleContents().forEach(f->{
                f.setAccountId(currentAccountId);
                f.setCompanyId(userInfo.getCompanyId());
                f.setBarcodeRuleId(intValue);
            });
            barcodeRuleBoService.saveErpBarcodeRuleBo(erpBarcodeRule.getRuleBos(),mainId);
            barcodeRuleContentService.saveRuleContent(erpBarcodeRule.getRuleContents(),mainId);
        }
        return mainId;
    }

    @Override
    public Page<ErpBarcodeRule> pageByBarcodeBo(Page page, ErpBarcodeRuleSearchMoreDto barcodeRuleSearchMoreDto) {
        UserInfo userInfo = UserThreadLocal.get();
        Page<ErpBarcodeRule> erpBarcodeRulePage = baseMapper.pageByBarcodeBo(page, userInfo.getCompanyId(), barcodeRuleSearchMoreDto);
        return erpBarcodeRulePage;
    }

    @Override
    public void deleteByIds(Long... ids) {
        if(null == ids)throw new NuoNiuException("请选择记录");
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.in("id",ids);
        remove(queryWrapper);
    }

    @Override
    public void examine(Long id, Integer status) {
        if(status != 0 && status != 1)throw new NuoNiuException("参数错误");
        ErpBarcodeRule barcodeRule = getById(id);
        Integer oldStatus = barcodeRule.getStatus();
        if(oldStatus > 0 && status == 1)throw new NuoNiuException("已经审核");
        if(oldStatus == 0 && status == 0)throw new NuoNiuException("已经是待审核状态");
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",status);
        update(updateWrapper);
    }

    @Override
    public ErpBarcodeRule detailById(Long id,boolean isSearchBo) {
        ErpBarcodeRule barcodeRule = getById(id);
        List<ErpBarcodeRuleContent> erpBarcodeRuleContents = barcodeRuleContentService.listByRuleId(id);
        barcodeRule.setRuleContents(erpBarcodeRuleContents);
        if(isSearchBo){
            List<ErpBarcodeRuleBo> erpBarcodeRuleBos = barcodeRuleBoService.listByRuleId(id);
            barcodeRule.setRuleBos(erpBarcodeRuleBos);
        }
        return barcodeRule;
    }


}
